home *** CD-ROM | disk | FTP | other *** search
/ Software 2000 / Software 2000 Volume 1 (Disc 1 of 2).iso / utilities / u264.dms / in.adf / ARPPRO3.0 / PRO.RUN / scdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-18  |  2.3 KB  |  128 lines

  1. /* Copyright (C) 1986,1987 Manx Software Systems, Inc.  */
  2.  
  3. /* not exactly re-entrant, is it?  it does make a simple directory scanner though */
  4.  
  5. #include <exec/memory.h>
  6. #include <libraries/arpbase.h>
  7. #include <user/ftype.h>
  8. #include <functions.h>
  9. #include <errno.h>
  10.  
  11. #define MAXNAMELEN 256
  12.  
  13. struct AnchorPath *findfirst(), *findnext();
  14.  
  15. char *scdir (pat)
  16. char *pat;
  17. {
  18.     register struct AnchorPath *ap;
  19.     static int time = 0;
  20.  
  21.     Chk_Abort();
  22.  
  23.     do {
  24.     if (!time) {        /* new pattern */
  25.         time = 1;
  26.         ap = findfirst(pat);
  27.     }
  28.     else {            /* continue pattern */
  29.         ap = findnext();
  30.     }
  31.     } while (ap && arpisdir(ap));
  32.  
  33.     if (!ap) {          /* no more (return null) */
  34.     time = 0;
  35.     return NULL;
  36.     }
  37.  
  38.     return ap->ap_Buf;    /* return ptr to name */
  39. }
  40.  
  41.  
  42. /* static struct DefaultTracker *_tr,*_tr2;  */
  43. static struct AnchorPath *_ap;
  44.  
  45. static
  46. struct AnchorPath *findfirst(path)
  47. char *path;
  48. {
  49.     struct AnchorPath *findrtn();
  50.     struct AnchorPath *allocap();
  51.  
  52.     if (!_ap && !allocap()) return NULL;
  53.  
  54.     return findrtn (FindFirst (path,_ap));
  55. }
  56.  
  57. static
  58. struct AnchorPath *findnext()
  59. {
  60.     struct AnchorPath *findrtn();
  61.  
  62.     return findrtn (FindNext (_ap));
  63. }
  64.  
  65. static
  66. struct AnchorPath *findrtn(rc)
  67. ULONG rc;
  68. {
  69.     switch (rc) {
  70.     case 0:
  71.         return _ap;
  72.  
  73.     case ERROR_BREAK:
  74.         _abort();
  75.     case ERROR_NO_MORE_ENTRIES:
  76.         errno = 0;
  77.         break;
  78.  
  79.     default:
  80.         errno = rc;
  81.         break;
  82.     }
  83.  
  84.     FreeAnchorChain (_ap);
  85.  
  86.     return NULL;
  87. }
  88.  
  89.  
  90. static
  91. struct AnchorPath *allocap()
  92. {
  93.     register struct AnchorPath *ap = NULL;
  94.  
  95.     if ( (ap = ArpAlloc((long)sizeof *ap + MAXNAMELEN)) ) {
  96.     SET_ID(ap,TRAK_ANCHOR);
  97.  
  98.     if (Enable_Abort) ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  99.     ap->ap_StrLen = MAXNAMELEN;
  100.     _ap = ap;
  101.     }
  102.     else errno = ENOMEM;
  103.  
  104.     return ap;
  105.  
  106. #if 0
  107.     register struct DefaultTracker *tr;
  108.     register struct AnchorPath *ap = NULL;
  109.  
  110.     /* !!! the order of these two allocations is VERY important -
  111.            unless done in this order, FreeTaskResList() will not be
  112.            able to call FreeAnchorChain() correctly */
  113.  
  114.     if ( (ap = ArpAlloc((long)sizeof *ap + MAXNAMELEN)) && (tr = GetTracker(TRAK_ANCHOR)) ) {
  115.     tr->dt_Object.dt_Resource = (CPTR)ap;
  116.     if (Enable_Abort) ap->ap_BreakBits = SIGBREAKF_CTRL_C;
  117.     ap->ap_StrLen = MAXNAMELEN;
  118.     _ap = ap;
  119.     }
  120.     else {
  121.     if (tr) FreeTrackedItem(tr);
  122.     errno = ENOMEM;
  123.     }
  124.     return ap;
  125. #endif
  126.  
  127. }
  128.